package jvProjects.imageFusion;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import deformation.DeformationGenerator;
import deformation.DeformationGenerator.DeformationType;
import jv.number.PuDouble;
import jv.object.PsDebug;
import jv.object.PsUpdateIf;
import jv.project.PjProject_IP;
import jvDeformation.gui.ComboBox;
import jvDeformation.gui.JvDeformationDisplayPanel;
import jvDeformation.gui.RadioBox;

/**
 * Info panel of tutorial project. Each project, as well as each geometry, may have
 * an info panel for inspecting and steering the instance. Modifications in the panel
 * should result in a call to <code>project.update(Object)</code>.
 * <p>
 * Panel adds simple sliders for modifying integer values of the project. The sliders
 * are children of the project and send <code>update(Object)</code> whenever they change
 * by user interaction.
 * <p>
 * This info panel is optional and can be removed without any changes in the project.
 * Each info panel must follow special naming conventions such that JavaView is able to
 * show it automatically, or on request. The class name must be identical to the class
 * name of the corresponding class with the suffix '_IP' attached. IP means InfoPanel.
 * 
 * @see			jv.project.PjProject
 * @author		Konrad Polthier
 * @version		28.11.00, 1.50 revised (kp) Applet simplified for beginners.<br>
 *					20.03.00, 1.10 revised (kp) Sliders moved to project.<br>
 *					05.08.99, 1.00 created (kp)
 */
public class PjFusion_IP extends PjProject_IP implements ActionListener {
	protected	PjFusion		imageDeformationProject;
	protected	Panel				sliderPanel;
	protected	Label				lArea;
	
	
	
	// Declare your GUI objects here, e.g. a button
	protected 	Button				bLoad1;
	protected 	Button				bLoad2;
	protected 	Button				bStick;
	protected 	Button				bTest;
	protected 	Button				bopenTwo;
	protected 	Button				bfuse;
	protected   PuDouble            dFusionGlider;
	protected   PuDouble            dFusionGlider_factor;
	protected   PuDouble            dFusionGlider_color;
	protected 	Button				bsave;
	protected 	Button				bsave_gif;
	protected   ComboBox<DeformationType>            cDeformationType;
	boolean sticked =false;
	private JvDeformationDisplayPanel ddp = new JvDeformationDisplayPanel();
	public PjFusion_IP() {
		super();
		// Add title label which will later display the title of the project.
		addTitle("");


		// The GUI object declared above needs to be created here
		bLoad1 = new Button("Load Image 1");
		bLoad1.addActionListener(this);
		bLoad2 = new Button("Load Image 2");
		bLoad2.addActionListener(this);
		bopenTwo = new Button("Load Two");
		bopenTwo.addActionListener(this);
		bfuse = new Button("fuse");
		bfuse.addActionListener(this);
		
		cDeformationType = new ComboBox<DeformationType>(DeformationGenerator.getAllDeformations());
		bStick = new Button("Stick");
		bStick.addActionListener(this);
		bTest = new Button("Test");
		bTest.addActionListener(this);
		dFusionGlider = new PuDouble("fusion");
		dFusionGlider.setBounds(0, 1, 0.01, 0.01);
		dFusionGlider.setValue(0.5);
		dFusionGlider.addUpdateListener(this);
		dFusionGlider.setEnabledConfigButton(false);
		dFusionGlider.setEnabled(false);
		dFusionGlider_factor = new PuDouble("fusion factor");
		dFusionGlider_factor.setBounds(0, 1, 0.01, 0.01);
		dFusionGlider_factor.setValue(0.5);
		dFusionGlider_factor.addUpdateListener(this);
		dFusionGlider_factor.setEnabledConfigButton(false);
		dFusionGlider_factor.setEnabled(false);
		dFusionGlider_color = new PuDouble("fusion color");
		dFusionGlider_color.setBounds(0, 1, 0.01, 0.01);
		dFusionGlider_color.setValue(0.5);
		dFusionGlider_color.addUpdateListener(this);
		dFusionGlider_color.setEnabledConfigButton(false);
		dFusionGlider_color.setEnabled(false);
		bsave = new Button("Save");
		bsave.addActionListener(this);
		bsave.setEnabled(false);
		bsave_gif = new Button("Save as gif");
		bsave_gif.addActionListener(this);
		bsave_gif.setEnabled(false);
		add(bLoad1);
		add(bLoad2);
		add(ddp );
		add(bfuse);
		add(dFusionGlider.getInfoPanel());
		add(dFusionGlider_factor.getInfoPanel());
		add(dFusionGlider_color.getInfoPanel());
		add(bsave);
		add(bsave_gif);
		
		if (getClass() == PjFusion_IP.class)
			init();
	}
	
	public void setParent(PsUpdateIf parent) {
		super.setParent(parent);
		imageDeformationProject = (PjFusion)parent;
		setTitle(imageDeformationProject.getName());
		ddp.setJvDeformationDisplay(imageDeformationProject.defdis1);
	}
	
	/**
	* Here we arrive from outside world of this panel, e.g. if
	* project has changed somewhere else and must update its panel. Such an update
	* is automatically by superclasses of PjProject.
	*/
	public boolean update(Object event) {
		if (imageDeformationProject==null) {
			if (PsDebug.WARNING) PsDebug.warning("missing parent, setParent not called");
			return false;
		}
		if (event == dFusionGlider_factor) {
			imageDeformationProject.setFusionFactor(dFusionGlider_factor.getValue(), dFusionGlider_color.getValue());
		}else if (event == dFusionGlider_color) {
			imageDeformationProject.setFusionFactor(dFusionGlider_factor.getValue(), dFusionGlider_color.getValue());
		}else if (event == dFusionGlider) {
			dFusionGlider_color.setValue(dFusionGlider.getValue());
			dFusionGlider_factor.setValue(dFusionGlider.getValue());
			imageDeformationProject.setFusionFactor(dFusionGlider_factor.getValue(), dFusionGlider_color.getValue());
		}
		return super.update(event);
	}
	/**
	 * Handle action events invoked from buttons, menu items, text fields.
	 */
	public void actionPerformed(ActionEvent event) {
		if (imageDeformationProject==null)
			return;
		Object source = event.getSource();
		if(source == bLoad1){
			imageDeformationProject.loadImage1();
		}else if (source == bLoad2) {
			imageDeformationProject.loadImage2();
		}else if (source == bfuse) {
			imageDeformationProject.fuse();
			dFusionGlider_factor.setEnabledConfigButton(true);
			dFusionGlider_factor.setEnabled(true);
			dFusionGlider_color.setEnabledConfigButton(true);
			dFusionGlider_color.setEnabled(true);
			dFusionGlider.setEnabledConfigButton(true);
			dFusionGlider.setEnabled(true);
			bsave.setEnabled(true);
			bsave_gif.setEnabled(true);
	}	else if(source == bsave){
		bsave.setEnabled(false);
		imageDeformationProject.saveImage();
		bsave.setEnabled(true);
	}else if(source == bsave_gif){
		bsave_gif.setEnabled(false);
		imageDeformationProject.saveImage_gif();
		bsave_gif.setEnabled(true);
	}
	}
}

